<%--
  Created by IntelliJ IDEA.
  User: sofwin
  Date: 2021/3/26
  Time: 10:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%String path = request.getContextPath()+"/";%>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <base href="<%=path%>">
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="layui/css/font.css">
    <link rel="stylesheet" href="layui/css/xadmin.css">
    <script src="layui/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="layui/js/xadmin.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="x-nav">
    <form class="layui-form  layui-col-space5" action="system/user/data" method="post" id="form">

          <span class="layui-breadcrumb">
            <a href="">首页</a>
            <a href="">系统设置</a>
            <a>
              <cite>用户管理</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                      <input type="hidden" name="pageNumber" value="${page.pageNum}" id="pageNumber"/>
                        <input type="hidden" name="pageSize" value="${page.pageSize}"/>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" name="loginAccount" value="${user.loginAccount}" placeholder="请输入关键字" class="layui-input search_input">
                        </div>
                        <shiro:hasPermission name="user:query">
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn search"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                            </div>
                        </shiro:hasPermission>
                </div>
                <div class="layui-card-header">
                    <shiro:hasPermission name="user:deletes">
                         <button class="layui-btn layui-btn-danger batchDel" type="button"><i class="layui-icon"></i>批量删除</button>
                    </shiro:hasPermission>
                    <shiro:hasPermission name="user:add">
                        <button class="layui-btn usersAdd_btn" type="button"><i class="layui-icon"></i>添加</button>
                    </shiro:hasPermission>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form">
                        <colgroup>
                            <col width="50">
                            <col>
                            <col width="18%">
                            <col width="8%">
                            <col width="12%">
                            <col width="12%">
                            <col width="12%">
                        </colgroup>
                        <thead>
                        <tr>
                            <th><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose" id="allChoose"></th>
                            <th>登录名</th>
                            <th>姓名</th>
                            <th>所属角色</th>
                            <th>所属部门</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody class="users_content">
                        <!--循环显示数据 -->
                        <c:choose>
                            <c:when test="${fn:length(data)==0}">
                                <tr>
                                    <td colspan="7">暂无数据</td>
                                </tr>
                            </c:when>
                            <c:otherwise>
                                <c:forEach items="${data}" var="user" varStatus="t">
                                    <tr>
                                        <td><input type="checkbox" lay-filter="idChoose" name="ids" lay-skin="primary"  value="${user.id}" id="checkbox_${user.id}"></td>
                                        <td>${user.loginAccount}</td>
                                        <td>${user.realName}</td>
                                        <td>${user.role.roleName}</td>
                                        <td>${user.dept.deptName}</td>
                                        <td class="layui-form">
                                            <input type="checkbox" data="${user.id}" lay-filter="ofilter" name="zzz" lay-skin="switch" lay-text="启用|停用" <c:if test="${user.flag==1}">checked</c:if>/>
                                        </td>
                                        <td>
                                            <shiro:hasPermission name="user:edit">
                                                <a title="编辑" href="javascript:void(0)" class="edit" data="${user.id}">
                                                    <i class="layui-icon">&#xe642</i>
                                                </a>
                                            &nbsp;&nbsp;</shiro:hasPermission>
                                            <shiro:hasPermission name="user:delete">
                                            <a href="javascript:void(0)" title="删除" class="delete" data="${user.id}">
                                                <i class="layui-icon">&#xe640;</i>
                                            </a>
                                            </shiro:hasPermission>
                                        </td>
                                    </tr>
                                </c:forEach>
                            </c:otherwise>
                        </c:choose>
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page" id="page">

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</form>
</body>
<script>
    layui.use(['laydate','form','layer','jquery','laypage'], function(){

        var laydate = layui.laydate;
        var form = layui.form;
        layer = layui.layer;
        $=layui.jquery;
        laypage = layui.laypage;
        // 编辑
        $(".edit").click(function () {
            let id = $(this).attr("data");
            index = layer.open({
                title:'添加用户信息',
                type:2,
                content:'system/user/toEdit?id='+id,
                area:['525px','449px']
            });
        });
        form.on('checkbox(idChoose)',function(data){
            // id的所有多选框的个数
            var allIds= $("input[name='ids']").length;
            // id所有多选框中选中的个数
            var allIdsChecked=$("input[name='ids']:checked").length;
            // 选中的《总个数，说明不是全选
            if(parseInt(allIdsChecked)<allIds){
                $("#allChoose").removeAttr('checked');
            }else{
                $("#allChoose")[0].checked=true;
            }
            form.render();
        });
        form.on('checkbox(allChoose)', function(data){
            $("input[name='ids']").each(function(){
                this.checked=data.elem.checked
            })
            form.render();
        });
        // 分页
        // 渲染分页栏
        //执行一个laypage实例
        laypage.render({
            elem:'page',
            count:parseInt('${page.total}'),
            limit:parseInt('${page.pageSize}'),
            limits:[10,20,30],
            curr:${page.pageNum},
            layout:['prev', 'page', 'next','skip'],
            jump:function(obj,first){
                if(!first){
                    // 手动在切换页码
                    $("#pageNumber").val(obj.curr);
                    $("#form").submit();
                }
            }
        })
        // 医用停用
        form.on('switch(ofilter)', function(data){
            let id = $(this).attr("data");
            let flag =0;
            if(data.elem.checked){
                flag=1;
            }
            $.ajax({
                url:'system/user/save?id='+id+'&flag='+flag,
                type:'get',
                dataType:'json',
                success:function(ret){
                    if(!ret.status){
                        layer.msg('操作失败!');
                    }
                }
            })
        });
        // 查询
        $(".search").click(function(){
            $("#form").submit();
        });
        // 新增弹出层
        $(".usersAdd_btn").click(function(){
            index = layer.open({
                title:'添加用户信息',
                type:2,
                content:'system/user/toEdit',
                area:['525px','449px']
            });
        });
        // 单个删除
        $(".delete").click(function(){
            var id = $(this).attr("data");
            layer.confirm('是否确定删除?', {icon: 3, title:'提示'}, function(index){
                $.ajax({
                    url:'system/user/deletes',
                    type:'post',
                    dataType:'json',
                    data:{
                        ids:id
                    },
                    success:function(ret){
                        if(ret.status){
                            layer.msg('删除成功!',{
                                offset: 't',
                                time:2000 ,
                                anim: 6
                            },function(){
                                $("#form").submit();
                            })

                        }
                    }
                })
            });
        })
        // 批量删除
        $(".batchDel").click(function(){
            if($("input[name='ids']:checked").length==0){
                layer.msg('未选中需要删除的数据');
                return;
            }
            layer.confirm('是否确定删除'+$("input[name='ids']:checked").length+'个数据?', function(index){

                var formParams = $("#form").serialize();
                $.ajax({
                    url:'system/user/deletes',
                    type:'post',
                    dataType:'json',
                    data:formParams,
                    success:function(ret){
                        if(ret.status){
                            layer.msg('删除成功!',{
                                offset: 't',
                                time:2000 ,
                                anim: 6
                            },function(){
                                $("#form").submit();
                            })

                        }
                    }
                })
            });
        })
    });







</script>
</html>